package javaapplication1;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;

import org.w3c.tidy.Tidy;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Admin
 */
public class NewClass {
    private String url;
    private String outFileName;
    private String errOutFileName;
            
    public NewClass(String url, String outFileName, String errOutFileName) 
    {
    this.url = url;
    this.outFileName = outFileName;
    this.errOutFileName = errOutFileName;
    }
    public static void URL2XML(String address, String xmlFileName) throws Exception {

        URL url = new URL(address);
        BufferedReader br = new BufferedReader( new InputStreamReader(
                url.openStream(), "UTF-8"));
        String s = "";
        while (true) {
            String str = br.readLine();
            if (str == null) {
                break;
            } else {
                s += str + "\n";
            }
        }
        br.close();

        if (!xmlFileName.endsWith(".xml")) {
            xmlFileName += ".xml";
        }
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(
                new FileOutputStream(xmlFileName), "utf-8"));
        bw.write(chuyenHTMLXML.Html2Xml(s));
        bw.close();
    }
    public void run() 
    {
        URL u;
        BufferedInputStream in;
        FileOutputStream out;

        Tidy tidy = new Tidy();

        //Tell Tidy to convert HTML to XML
        //tidy.setIndentCdata(true);
        tidy.setPrintBodyOnly(true);
        //tidy.setFixUri(true);
        //tidy.setHideComments(true);
        tidy.setMakeClean(true);
        // System.out.println("here");
        //tidy.setForceOutput(true);
        tidy.setXmlOut(true);
        System.out.println("Done: "+this.url);
        try {
        //Set file for error messages
        tidy.setErrout(new PrintWriter(new FileWriter(errOutFileName), true));
        u = new URL(url);

        //Create input and output streams
        in = new BufferedInputStream(u.openStream());                
        out = new FileOutputStream(outFileName);

        //Convert files
        tidy.parse(in, out);

        //Clean up
        in.close();
        out.close();

    } catch (IOException e) {
    System.out.println(this.toString() + e.toString());
    }
    }
    
}
